package com.dg11185.nearshop.dbHelper;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.dg11185.libs.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.dg11185.libs.ormlite.dao.Dao;
import com.dg11185.libs.ormlite.support.ConnectionSource;
import com.dg11185.libs.ormlite.table.TableUtils;
import com.dg11185.nearshop.db.entity.LastUpdateTime;
import com.dg11185.nearshop.db.entity.Location;
import com.dg11185.nearshop.db.entity.Merchant;
import com.dg11185.nearshop.net.entity.Area;
import com.dg11185.nearshop.net.entity.Industry;
import com.dg11185.nearshop.net.weixin.Tabloid;

/**
 * 
 * @author：Daibo    
 * @time：2014-6-2 下午5:47:48
 */
public class OrmLiteHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "DGPOST_NearShop.db";

    private static final int DATABASE_VERSION = 3;

    /** 记录各个数据库表的最后更新时间表 */
    private Dao<LastUpdateTime, Integer> lastUpdateTimeDao = null;

    /** 记录行业分类表 */
    private Dao<Industry, Integer> industryDao = null;

    /** 记录城市商圈表 */
    private Dao<Area, Integer> areaDao = null;

    /**记录用户最近浏览商家表*/
    private Dao<Merchant, Integer> merchantDao = null;

    /**保存地理位置表 只有一行数据*/
    private Dao<Location, Integer> locationDao = null;
    
    //保存小报消息的表
    private Dao<Tabloid, Integer> tabloidDao = null;
    
    public OrmLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, LastUpdateTime.class);
            TableUtils.createTable(connectionSource, Industry.class);
            TableUtils.createTable(connectionSource, Area.class);
            TableUtils.createTable(connectionSource, Merchant.class);
            TableUtils.createTable(connectionSource, Location.class);
            TableUtils.createTable(connectionSource, Tabloid.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * {@link OrmLiteSqliteOpenHelper#onUpgrade(SQLiteDatabase, ConnectionSource, int, int)}
     */
    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, LastUpdateTime.class, true);
            TableUtils.dropTable(connectionSource, Industry.class, true);
            TableUtils.dropTable(connectionSource, Area.class, true);
            TableUtils.dropTable(connectionSource, Merchant.class, true);
            TableUtils.dropTable(connectionSource, Location.class, true);
            TableUtils.dropTable(connectionSource, Tabloid.class, true);
            onCreate(database, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public Dao<LastUpdateTime, Integer> getLastUpdateTimeDao() {
        if (lastUpdateTimeDao == null) {
            try {
                lastUpdateTimeDao = getDao(LastUpdateTime.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return lastUpdateTimeDao;
    }
    
    
    public Dao<Industry, Integer> getIndustryDao() {
        if (industryDao == null) {
            try {
                industryDao = getDao(Industry.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return industryDao;
    }

    public Dao<Area, Integer> getAreaDao() {
        if (areaDao == null) {
            try {
                areaDao = getDao(Area.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return areaDao;
    }

    public Dao<Merchant, Integer> getMerchantDao() {
        if (merchantDao == null) {
            try {
                merchantDao = getDao(Merchant.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return merchantDao;
    }

    public Dao<Location, Integer> getLocationDao() {
        if (locationDao == null) {
            try {
                locationDao = getDao(Location.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return locationDao;
    }

    public Dao<Tabloid, Integer> getTabloidDao() {
        if (tabloidDao == null) {
            try {
                tabloidDao = getDao(Tabloid.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return tabloidDao;
    }
    
    @Override
    public void close() {
        lastUpdateTimeDao = null;
        industryDao = null;
        areaDao = null;
        merchantDao = null;
        locationDao = null;
        tabloidDao = null;
        super.close();
    }
}
